﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.ModLoader;

namespace MoreEvent.Content.Projectiles
{
    public class Knife : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            ProjectileID.Sets.TrailCacheLength[Projectile.type] = 9;
            ProjectileID.Sets.TrailingMode[Projectile.type] = 0;
        }
        public override void SetDefaults()
        {
            Projectile.width = 14;
            Projectile.timeLeft = 6000;
            Projectile.CloneDefaults(81);
            Projectile.height = 14;
            Projectile.penetrate = 3;
        }
        public override bool PreAI()
        {
            Projectile.rotation += 0.3f;
            return true;
        }
        public override bool OnTileCollide(Vector2 oldVelocity)
        {
            SoundEngine.PlaySound(SoundID.Item48, new Vector2(Projectile.position.X, Projectile.position.Y));
            int num = DustID.Iron;
            for (int i = 0; i < 6; i++)
            {
                Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
                Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
            }
            Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
            Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
            Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
            Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
            Projectile.penetrate--;
            if (Projectile.penetrate <= 0)
            {
                Projectile.Kill();
            }
            else
            {
                Projectile.ai[0] += 0.1f;
                if (Projectile.velocity.X != oldVelocity.X)
                {
                    Projectile.velocity.X = -oldVelocity.X;
                }
                if (Projectile.velocity.Y != oldVelocity.Y)
                {
                    Projectile.velocity.Y = -oldVelocity.Y;
                }
                Projectile.velocity *= 0.15f;
            }
            return false;
        }
        public override void OnKill(int timeLeft)
        {
            SoundEngine.PlaySound(SoundID.Item50, new Vector2(Projectile.position.X, Projectile.position.Y));
            int num = DustID.Iron;
            for (int i = 0; i < 6; i++)
            {
                Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
                Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
            }
            Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
            Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
            Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
            Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, num, 2.5f, -2.5f, 0, Color.White, 0.7f);
        }
        public override bool PreDraw(ref Color lightColor)
        {
            Vector2 vector;
            vector = new(TextureAssets.Projectile[Projectile.type].Value.Width * 0.5f, Projectile.height * 0.5f);
            for (int i = 0; i < Projectile.oldPos.Length; i++)
            {
                Vector2 vector2 = Projectile.oldPos[i] - Main.screenPosition + vector + new Vector2(0f, Projectile.gfxOffY);
                Color color = Projectile.GetAlpha(lightColor) * ((Projectile.oldPos.Length - i) / (float)Projectile.oldPos.Length);
                Main.spriteBatch.Draw(TextureAssets.Projectile[Projectile.type].Value, vector2, null, color, Projectile.rotation, vector, Projectile.scale, 0, 0f);
            }
            return true;
        }
    }
}
